﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Linq;
using System.Web;
using System.Text;

namespace Rssc.administrator.app_data
{
    public class CPublic
    {
        //Tạo mã bâm
        public static string Encode(string ValueInput)
        {
            //Chuyen chuoi ky tu thanh mang so nguyen
            byte[] bytes = Encoding.UTF8.GetBytes(ValueInput);

            //Tao lop MD5 cua NET
            MD5 md5 = new MD5CryptoServiceProvider();

            //Thuc thi phan ma hoa => mang byte da bam
            byte[] hash = md5.ComputeHash(bytes);

            //Chuyen thanh chuoi ky tu
            StringBuilder sb = new StringBuilder();
            foreach(byte b in hash)
            {
                sb.Append(string.Format("{0:x2}", b));
            }

            return sb.ToString();
        }

        public static bool Decode(string strInput, string strHash)
        {
            return string.Equals(Encode(strInput), strHash, StringComparison.OrdinalIgnoreCase);
        }

        public static string Page_JavaScript()
        {
            string _result = string.Empty;
            _result += "<script type=\"text/javascript\">\n";
            _result += "\t$(document).ready(function () {\n";
            _result += "\t\t$('#control-bar .float-right').hide();\n";
            _result += "\t});\n";
            _result += "</script>\n";
            return _result;
        }

        //Split word
        private static string[] SplitWords(string s)
        {
            return Regex.Split(s, @"\W+");
        }

        public static string SplitTitle(string title, int num_word)
        {
            string result = "";
            int i = 0;
            string[] w = SplitWords(title);

            foreach (string s in w)
            {
                if (i > num_word)
                {
                    result += "...";
                    return result;
                }
                else
                {
                    result += s + " ";
                }

                i++;
            }

            return title;
        }

        public static string SplitChar(string value, int num)
        {
            string _result = string.Empty;
            int i = 0;
            if (value.Count() > num)
            {
                foreach (char c in value)
                {
                    if (i > num)
                        break;
                    _result += c;
                    i++;

                }
                return _result + "...";
            }
            return value;
        }

        public static string ChangePathImge(string path)
        {
            return path.Replace('/', '/');
        }

        public static string PathSplit(string path)
        {
            return path.Replace("\\", "-");
        }

        public static string ContentFolderRight_Load(string _path)
        {
            string _result = string.Empty;

            FileSystemInfo[] _files = new DirectoryInfo(_path).GetFileSystemInfos();

            var _sortType = from ex in _files
                            orderby ex.Attributes ascending
                            select ex;
            _result += "\t\t\t<ul id=\"rssc-content-folder\" class=\"grid dark-grey-gradient\">\n";
            foreach (var item in _sortType)
            {
                if (item.Attributes == FileAttributes.Directory)
                {
                    _result += "\t\t\t\t<li>\n";
                    _result += "\t\t\t\t\t<div class=\"grid-picto user\" style=\"background:none\">\n";
                    _result += "\t\t\t\t\t\t<img src=\"../administrator/images/folder.gif\"  height=\"100\" width=\"100\" />";
                    _result += "\t\t\t\t\t\t<small>" + CPublic.SplitChar(item.Name, 10) + "</small>\n";
                    _result += "\t\t\t\t\t</div>";
                    _result += "\t\t\t\t\t<ul class=\"grid-actions\">\n";
                    _result += "\t\t\t\t\t\t<li><a class=\"with-tip\" title=\"Delete\" href=\"#\"><img height=\"16\" width=\"16\" src=\"images/icons/fugue/cross-circle.png\"></a></li>\n";
                    _result += "\t\t\t\t\t\t<li><input type=\"checkbox\" value=\"2\" id=\"grid-selected-2\" name=\"selected[]\"></li>\n";
                    _result += "\t\t\t\t\t</ul>";
                    _result += "\t\t\t\t</li>\n";
                }
                else
                {
                    string _urlPath = HttpContext.Current.Request.Url.AbsoluteUri;
                    string[] _urls = _urlPath.Split('/');
                    _result += "\t\t\t\t<li>\n";
                    _result += "\t\t\t\t\t<div class=\"grid-picto user\" style=\"background:none\">\n";
                    int[] _size = CheckImageSize(item.FullName);
                    _result += "\t\t\t\t\t\t<img src=\"" + ProcessPath(item) + "\"  height=\"" + _size[0] + "\" width=\"" + _size[1] + "\" />";
                    _result += "\t\t\t\t\t\t<small>" + CPublic.SplitChar(item.Name, 10) + "</small>\n";
                    _result += "\t\t\t\t\t</div>";
                    _result += "\t\t\t\t\t<ul class=\"grid-actions\">\n";
                    _result += "\t\t\t\t\t\t<li><a class=\"with-tip\" title=\"Delete\" href=\"#\"><img height=\"16\" width=\"16\" src=\"images/icons/fugue/cross-circle.png\"></a></li>\n";
                    _result += "\t\t\t\t\t\t<li><input type=\"checkbox\" value=\"2\" id=\"grid-selected-2\" name=\"selected[]\"></li>\n";
                    _result += "\t\t\t\t\t</ul>";
                    _result += "\t\t\t\t</li>\n";
                }
            }
            _result += "\t\t\t</ul>\n";

            return _result;
        }

        //Check image size
        private static int[] CheckImageSize(string fileName)
        {
            int[] _result = new int[2];
            System.Drawing.Image _image = default(System.Drawing.Image);
            _image = System.Drawing.Image.FromFile(fileName);
            int _width = Convert.ToInt32(_image.Width);
            int _height = Convert.ToInt32(_image.Height);
            if (_width > 100 && _height > 100)
            {
                _result[0] = _result[1] = 100;
            }
            else
            {
                _result[0] = _width;
                _result[1] = _height;
            }
            return _result;
        }

        //Path Image
        private static string ProcessPath(FileSystemInfo fileInfo)
        {
            string _result = string.Empty;
            string _url = HttpContext.Current.Request.Url.AbsoluteUri;
            string[] _com = _url.Split('/');
            _result += _com[0] + "//" + _com[2];

            string _fullname = fileInfo.FullName;
            string[] _arrFileName = _fullname.Split('\\');
            for (int i = 0; i < _arrFileName.Length; i++)
            {
                if (_arrFileName[i] == "images")
                {
                    for (int j = i; j < _arrFileName.Length; j++)
                    {
                        _result += "/" + _arrFileName[j];
                    }
                }
            }
            return _result;
        }
    }
}
